package rickyGame.server.state
{
	import flash.geom.Point;
	
	import ricky.utils.TimeServer;
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	
	import rickyGame.game.Fighter;
	import rickyGame.game.action.ActUtils;
	import rickyGame.game.skill.SkillData;
	import rickyGame.game.skill.SkillEffectTargetType;
	import rickyGame.server.SpellController;
	
	/**
	 * 执行自动释放技能永远不停歇
	 */
	public class AutoSpellAct extends Act2D
	{
		public function AutoSpellAct(actor:Fighter,skill:SkillData)
		{
			super(actor);
			_skill=skill;
			_fighter=actor;
			_point=new Point();
		}
		
		override public function begin():void
		{
			_states=_fighter.stateNames;
			_states.push(this);

			_last=TimeServer.now();
			_state=1;
			_interval=_skill.effectInterval;
			if(_interval==0)
				_interval=1000;
		}
		
		override public function action(timeline:Number):void
		{
			if(_state<1)
				return;
			
			var dt:Number=timeline-_last;
			if(dt>= _interval)//
			{
				_last=timeline;
				spell();
			}
		}
		
		override public function end():void
		{
			var i:int=_states.indexOf(this);
			if(i>=0)
				_states.splice(i,1);
			super.end();
		}
		
		private function spell():void
		{
			var x:Number,y:Number;
	
			x=actor.graphic.x;
			y=actor.graphic.y;
			
			_fighter.fightPro.spelling=true;
			if(!_fighter.fightCon)
				_fighter.fightCon=new SpellController(_fighter);
			
			_point.x=actor.graphic.x;
			_point.y=actor.graphic.y;
			//赛选目标

			var arr:Array=ActUtils.searchAround(_fighter,_skill,x,y);
			if(arr.length>0)
			{
				var tar:Fighter=arr[0];
				_fighter.fightCon.spell(tar,_point,_skill);
			}
		}
		
		//
		private var _states:Array;//状态
		private var _point:Point;//目标点
		private var _skill:SkillData;//技能数据
		private var _interval:Number;//执行间隔
		private var _last:Number;//上传使用时间
		private var _fighter:Fighter;//对象
	}
}